//
//  EJSGoodsVideoImageTableViewCell.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/26.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSGoodsVideoImageTableViewCell.h"
#import "XMUserTagsView.h"
#import "EJSGoodsModel.h"


@import AVKit;
@import AVFoundation;

@interface EJSGoodsVideoImageCCell:UICollectionViewCell

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIImageView *playImageView;

@end

@implementation EJSGoodsVideoImageCCell

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self addOwnViews];
    }
    return self;
}

- (void)addOwnViews{
    _imageView = [[UIImageView alloc] init];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_imageView];
    
    _playImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"com_pic_play"]];
    _playImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_playImageView];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _imageView.frame = self.bounds;
    [_playImageView sizeToFit];
    _playImageView.top = self.height/2 - _playImageView.height/2;
    _playImageView.left = self.width/2 - _playImageView.width/2;
}

@end


@interface EJSGoodsVideoImageTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    NSString *_videoUrl;
    EJSGoodsModel *_goodsModel;
}

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *goodsImageUrls;
@property (weak, nonatomic) IBOutlet XMUserTagsView *tagsView;

@end

@implementation EJSGoodsVideoImageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[EJSGoodsVideoImageCCell class] forCellWithReuseIdentifier:@"EJSGoodsVideoImageCCell"];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setGoodsVideoUrl:(NSString *)videoUrl imageUrls:(NSArray *)imageUrls{
    _videoUrl = videoUrl;
    self.goodsImageUrls = [NSMutableArray array];
    if(videoUrl && videoUrl.length > 0){
        [self.goodsImageUrls addObject:videoUrl];
    }
    
    
    if(_goodsModel.cover){
        [self.goodsImageUrls addObject:_goodsModel.cover];
    }
    [self.goodsImageUrls addObjectsFromArray:imageUrls];
    
    
    
    self.pageControl.numberOfPages = self.goodsImageUrls.count;
    if(self.goodsImageUrls.count > 1){
        self.pageControl.hidden = NO;
    }else{
        self.pageControl.hidden = YES;
    }
    
    
    
    [self.collectionView reloadData];
}

- (void)setGoodsModel:(EJSGoodsModel *)goodsModel{
    _goodsModel = goodsModel;
    [self setGoodsVideoUrl:goodsModel.video imageUrls:goodsModel.pics];
    
    NSMutableArray *tagsArray = [NSMutableArray array];
    
    BOOL shouldAddTag = NO;
    for (EJSRebateModel *item in goodsModel.coupon) {
        if (item.rebate.floatValue > 0) {
            shouldAddTag = YES;
            break;
        }
    }
    if (shouldAddTag) {
        [tagsArray addObject:@"返利"];
    }
    
    for(EJSLabelModel *labelModel in goodsModel.labels){
        if(labelModel.labelName){
            [tagsArray addObject:labelModel.labelName];
        }
    }
    [self.tagsView setUserTags:tagsArray];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.goodsImageUrls.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EJSGoodsVideoImageCCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSGoodsVideoImageCCell" forIndexPath:indexPath];
    if(_videoUrl && _videoUrl.length > 0 && indexPath.item == 0){
        ccell.playImageView.hidden = NO;
        [ccell.imageView setVideoImageWithUrl:_videoUrl];
    }else{
        ccell.playImageView.hidden = YES;
        [ccell.imageView setImageWithUrl:self.goodsImageUrls[indexPath.item]];
    }
    return ccell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(collectionView.width, collectionView.height);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(_videoUrl && _videoUrl.length > 0 && indexPath.item == 0){
        NSURL *url = [NSURL URLWithString:_videoUrl];
        AVPlayer *player = [AVPlayer playerWithURL:url];
        AVPlayerViewController *playerViewController = [AVPlayerViewController new];
        playerViewController.player = player;
        [[self getViewController] presentViewController:playerViewController animated:YES completion:nil];
        [playerViewController.player play];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger currentPage = scrollView.contentOffset.x/scrollView.width;
    self.pageControl.currentPage = currentPage;
}


@end
